package 练习增删改操作;

import JDBC工具包.JDBCPackage;
import 数据库操作.Find_;


import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.Scanner;

/**
 * @作者:侯国威
 * @时间: 2020/4/15/18:51
 * @邮箱:1045077189@qq.com
 */
public class index2 {
    public static void main(String[] args) {
        index2 in = new index2();
//        in.testInsert();
        in.Select();
    }

    //插入操作
    public void testInsert() {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入id:");
        int id = scanner.nextInt();

        System.out.println("请输入姓名:");
        String name = scanner.next();

        System.out.println("请输入班级:");
        String classid = scanner.next();

        System.out.println("请输入联系电话:");
        String phone = scanner.next();

        System.out.println("输入学号:");
        String number = scanner.next();

        System.out.println("输入寝室号:");
        String dormitroy = scanner.next();

        System.out.println("输入成绩:");
        String grade = scanner.next();

        String sql = "insert into `index`(id, name, classid, phone, number, dormitory,grade) VALUES (?,?,?,?,?,?,?)";
        int insertcount = update(sql, id, name, classid, phone, number, dormitroy, grade);
        if (insertcount > 0) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }

    }

    //1、添加一条数据
    public int update(String sql, Object... args) {
        Connection connection = null;

        PreparedStatement preparedStatement = null;

        try {
            connection = JDBCPackage.getconnection();

            preparedStatement = connection.prepareStatement(sql);

            //填充占位符
            for (int i = 0; i < args.length; i++) {

                preparedStatement.setObject(i + 1, args[i]);//参数声明错误
            }

            //方式二
            return preparedStatement.executeUpdate();
            //执行方式一
//            return preparedStatement.execute(); 在增删改时，没返回结果

        } catch (IOException | ClassNotFoundException | SQLException e) {

            e.printStackTrace();

        } finally {

            JDBCPackage.closeResource(connection, preparedStatement);

        }
        return 0;
    }


    //查询操作
    public void Select() {
        System.out.print("请输入查询类型:");
        System.out.print("a.学号:");
        System.out.print("b.姓名:");
        Scanner scanner = new Scanner(System.in);

        String sel = scanner.next();
        if ("a".equalsIgnoreCase(sel)) {
            System.out.print("请输入学号:");
            String number = scanner.next();

            String sql = "select id, name, classid, phone, number, dormitory,grade from `index` where number=?";
            student_info stu = getInstance(student_info.class, sql, number);
            if (stu != null) {
                System.out.println(stu);
            } else {
                System.out.println("请检查是否输入错误");
            }

        } else if ("b".equalsIgnoreCase(sel)) {
            System.out.print("请输入姓名:");
            String name = scanner.next();
            String sql = "select id, name, classid, phone, number, dormitory,grade from `index` where name=?";
            student_info info = getInstance(student_info.class, sql, name);
            if (info != null){
                System.out.println(info);
            }else {
                System.out.println("输入有错，请核对正确");
            }

        } else {
            System.out.println("");
        }
    }

    public <T> T getInstance(Class<T> clazz, String sql, Object... args) {

        Connection connection = null;

        PreparedStatement preparedStatement = null;

        ResultSet resultSet = null;
        try {
            connection = JDBCPackage.getconnection();

            preparedStatement = connection.prepareStatement(sql);

            //处理结果集一行数据中的每一个列
            for (int i = 0; i < args.length; i++) {

                //获取列值
                preparedStatement.setObject(i + 1, args[i]);
            }
            //结果集
            resultSet = preparedStatement.executeQuery();

            //ResultSetMetaData确定要使用的列名。列名是根据a确定的使用ResultSetMetaData查找
            //获取结果集的元数据

            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();

            int ColumnCount = resultSetMetaData.getColumnCount();//获取每一列
            //通过ResultSetMetaData获取结果集中的数据

            if (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < ColumnCount; i++) {
                    Object columvalue = resultSet.getObject(i + 1);//列值

                    //获取每个列的列名
                    String columnName = resultSetMetaData.getColumnName(i + 1);

                    //给 find_对象指定的某个对象，赋值为columvalue

                    Field field = clazz.getDeclaredField(columnName);

                    field.setAccessible(true);

                    field.set(t, columvalue);
                }
                System.out.println(t);
            }


        } catch (IOException | IllegalAccessException | IllegalArgumentException | NoSuchFieldException | ClassNotFoundException | SQLException | SecurityException | InstantiationException e) {

            e.printStackTrace();

        } finally {

            JDBCPackage.closeResourceInsert(connection, preparedStatement, resultSet);

        }

        return null;
    }

}
